using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
// hash the password so it doesn't show in log file or database
using System.Security.Cryptography;

namespace SimpleTCPClient
{
    class cApplication
    {
        /* NetworkStream that will be used */
        private static NetworkStream myStream;

       // System.Net.WebRequestCreate("http://naaaaak.com/~aaronds109/log.php?message="+ );

        /* TcpClient that will connect for us */
        private static TcpClient myClient;
        /* Storage space */
        private static byte[] myBuffer;
        /* Application running flag */
        private static bool bActive = true;

        /* Thread responsible for "remote input" */
        private static void ListenThread()
        {
            Console.WriteLine("Listening...");
            while (bActive)
            {
                /* Reading data from socket (stores the length of data) */
                int lData = myStream.Read(myBuffer, 0,myClient.ReceiveBufferSize);

                /* String conversion (to be displayed on console) */
                String myString = Encoding.ASCII.GetString(myBuffer);

                /* Trimming data to needed length, 
                   because TcpClient buffer is 8kb long */
                /* and we don't need that load of data 
                   to be displayed at all times */
                /* (this could be done better for sure) */
                myString = myString.Substring(0, lData);
                /* Display message */
                Console.Write(myString);
            }
        }

        /* Thread responsible for "local input" */
        private static void SendThread()
        {
            Console.WriteLine("Sending...");
            while (bActive)
            {
                /* Simple prompt */
                Console.Write("> ");
                /* Reading message/command from console */
                String myString = Console.ReadLine() + "\n";
                /* Sending the data */
                myStream.Write(Encoding.ASCII.GetBytes(
                     myString.ToCharArray()), 0, myString.Length);
            }
        }

        /* Entry point */
        static void Main(string[] args)
        {
            Console.Write("Enter server name/address: ");
            String strServer = Console.ReadLine();

            Console.Write("Enter remote port: ");
            String strPort = Console.ReadLine();

            /* Connecting to server (will crash if address/name is incorrect) */
            myClient = new TcpClient(strServer,Int32.Parse(strPort));
            Console.WriteLine("Connected...");

            /* Store the NetworkStream */
            myStream = myClient.GetStream();

            /* Create data buffer */
            myBuffer = new byte[myClient.ReceiveBufferSize];

            /* Vital: Create listening thread and assign it to ListenThread() */
            Thread tidListen = new Thread(new ThreadStart(ListenThread));
            /* Vital: Create sending thread and assign it to SendThread() */
            Thread tidSend = new Thread(new ThreadStart(SendThread));

            //Console.WriteLine("Application connected and ready...");
            //Console.WriteLine("----------------------------------");

            /* Start listening thread */
            tidListen.Start();
            /* Start sending thread */
            tidSend.Start();
            //login frame1 = new login();
            //frame1.Show();
        }
    }
}